/*
 * MessagesDao.java
 *
 * Created on November 9, 2008, 7:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.Members;
import jollylengkono.skripsi.model.Messages;

/**
 *
 * @author JollkY
 */
public class MessagesDao implements Crud {

    private int state;
    private Messages message;
    private List messages;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();

    /** Creates a new instance of MessagesDao */
    public MessagesDao() {
    }

    public int create(Object obj) {
        return 0;
    }
    
    public int send(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    public List<Object> getList() {
        messages = new ArrayList();
        try {
            em.getTransaction().begin();
            messages = em.createQuery("SELECT m FROM Messages m").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return messages;
    }
    
    public List getListByMember(long idMember) {
        messages = new ArrayList();
        try {
            em.getTransaction().begin();
            messages = em.createQuery("SELECT m FROM Messages m " +
                    "WHERE m.sender.id="+idMember).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return messages;
    }
    
    public Object read(long id) {
        message = new Messages();
        try {
            em.getTransaction().begin();
            message = em.find(Messages.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return message;
    }

    public int update(Object obj) {
        message = new Messages();
        try {
            Messages objMsg = (Messages) obj;
            em.getTransaction().begin();
            message = em.find(Messages.class, objMsg.getId());
            message.setAcceptDate(objMsg.getAcceptDate());
            message.setMessageStatus(objMsg.getMessageStatus());
            message.setRecipients(objMsg.getRecipients());
            message.setSender(objMsg.getSender());
            message.setSubject(objMsg.getSubject());
            message.setText(objMsg.getText());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    public int delete(long id) {
        message = new Messages();
        try {
            em.getTransaction().begin();
            message = em.find(Messages.class, id);
            em.remove(message);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
}
